

package cn.iocoder.yudao.module.apijson.framework;

import apijson.JSON;
import apijson.Log;
import apijson.NotNull;
import apijson.RequestMethod;
import apijson.orm.AbstractParser;
import apijson.orm.FunctionParser;
import apijson.orm.ObjectParser;
import apijson.orm.SQLConfig;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;


/**
 * 请求解析器
 *
 * @author Lemon
 */
@Getter
public class APIJSONParser extends AbstractParser<Long, JSONObject, JSONArray> {
    public static final String TAG = "APIJSONParser";


    public APIJSONParser() {
        super();
    }

    public APIJSONParser(RequestMethod method) {
        super(method);
    }

    public APIJSONParser(RequestMethod method, boolean needVerify) {
        super(method, needVerify);
    }


    @Override
    public APIJSONParser createParser() {
        return APIJSONApplication.createParser();
    }

    @Override
    public APIJSONFunctionParser createFunctionParser() {
        return APIJSONApplication.createFunctionParser();
    }

    @Override
    public APIJSONVerifier createVerifier() {
        return APIJSONApplication.createVerifier();
    }

    @Override
    public APIJSONSQLConfig createSQLConfig() {
        return APIJSONApplication.createSQLConfig();
    }

    @Override
    public APIJSONSQLExecutor createSQLExecutor() {
        return APIJSONApplication.createSQLExecutor();
    }

    @Override
    public APIJSONParser setNeedVerify(boolean needVerify) {
        super.setNeedVerify(needVerify);
        return this;
    }

    @Override
    public APIJSONParser setNeedVerifyLogin(boolean needVerifyLogin) {
        super.setNeedVerifyLogin(needVerifyLogin);
        return this;
    }

    @Override
    public APIJSONParser setNeedVerifyRole(boolean needVerifyRole) {
        super.setNeedVerifyRole(needVerifyRole);
        return this;
    }

    @Override
    public APIJSONParser setNeedVerifyContent(boolean needVerifyContent) {
        super.setNeedVerifyContent(needVerifyContent);
        return this;
    }

    @Override
    public JSONObject parseResponse(JSONObject request) {
        //补充format
        return super.parseResponse(request);
    }


    private FunctionParser<Long, JSONObject, JSONArray> functionParser;

    @Override
    public Object onFunctionParse(String key, String function, String parentPath, String currentName, JSONObject currentObject, boolean containRaw) throws Exception {
        if (functionParser == null) {
            functionParser = createFunctionParser();
            functionParser.setParser(this);
            functionParser.setMethod(getMethod());
            functionParser.setTag(getTag());
            functionParser.setVersion(getVersion());
            functionParser.setRequest(requestObject);
        }
        functionParser.setKey(key);
        functionParser.setParentPath(parentPath);
        functionParser.setCurrentName(currentName);
        functionParser.setCurrentObject(currentObject);

        return functionParser.invoke(function, currentObject, containRaw);
    }


    @Override
    public ObjectParser<Long, JSONObject, JSONArray> createObjectParser(@NotNull JSONObject request, String parentPath, SQLConfig<Long, JSONObject, JSONArray> arrayConfig
            , boolean isSubquery, boolean isTable, boolean isArrayMainTable) throws Exception {
        return new APIJSONObjectParser(request, parentPath, arrayConfig, isSubquery, isTable, isArrayMainTable) {
        }.setMethod(getMethod()).setParser(this);
    }

    /**
     * 校验角色及对应操作的权限
     */
    @Override
    public void onVerifyRole(@NotNull SQLConfig<Long, JSONObject, JSONArray> config) throws Exception {
        if (Log.DEBUG) {
            Log.i(TAG, "onVerifyRole  config = " + JSON.toJSONString(config));
        }
        if (isNeedVerifyRole()) {
            getVerifier().verifyAccess(config);
        }
    }

}
